<?php
  if(!$cms)
    include("../_cms/classes/cms.inc");

  if($cms->user['authorized']) {
    $ronde=$_GET['ronde'];
    // Store changed values
    if($cms->query->post['update'] && $cms->query->post['id']) {
      if($cms->query->post['keurmeesters_ronde2'])
        $cms->query->post['keurmeesters_ronde2'] = implode(',',$cms->query->post['keurmeesters_ronde2']);
      if($cms->query->post['keurmeesters_ronde3'])
        $cms->query->post['keurmeesters_ronde3'] = implode(',',$cms->query->post['keurmeesters_ronde3']);
      $result = $cms->db->update_tuple_array($cms->query->post, "inschrijving_bier", "inschrijving_bier",1);
      if($result->error)
        echo "<div class=\"form_error\">".$result->message."</div>\n";
      else
        echo "<div class=\"form_status\">".$result->message."</div>\n";
    }
    if(isset($cms->query->get['ajax_beer'])) {
      $beer = $cms->db->get_tuple_array('inschrijving_bier', $cms->query->get['ajax_beer']);
      unset($beer['inschrijving']);
      foreach($beer as $key=>$val)
        if(!$val) unset($beer[$key]);
      if($beer['keurmeesters_ronde2'])
        $beer['keurmeesters_ronde2'] = explode(',',$beer['keurmeesters_ronde2']);
      if($beer['keurmeesters_ronde3'])
        $beer['keurmeesters_ronde3'] = explode(',',$beer['keurmeesters_ronde3']);
      $cms->binary_buffer = json_encode($beer);
      return;
    }
    $todo=0;
    $sql = "SELECT count(*) tot FROM inschrijving_bier where punten=0 or punten is null";
    $result = $cms->db->execute($sql);          // Geeft een db_result object terug
    if ($result->num_rows) {
     $myrow = $result->get_nextrow();
     $todo = $myrow['tot'];
    }  
?>
<link rel="stylesheet" href="<?=$cms->contenturl;?>assets/css/onk.css" type="text/css" />
<link rel="stylesheet" href="<?=$cms->contenturl;?>assets/css/select2.css" type="text/css" />
<script type="text/javascript" src="<?=$cms->contenturl;?>assets/jquery.js"></script>
<script type="text/javascript" src="<?=$cms->contenturl;?>assets/select2.min.js"></script>


<div class="centerLayout jury">
<a href="home.php" style="center">
<h1 class="home_link">
  <img src="<?=$cms->contenturl;?>assets/gfx/logo.gif" style="vertical-align:middle" alt="ONK Labo logo">
  ONK jury
</h1>
<img src="<?=$cms->contenturl;?>assets/gfx/ONK-LABO-logo.png" class="logo_link" alt="ONK Labo logo">
</a>
<center><h2>Scores <?=$ronde?><sup>e</sup> ronde. Nog te scoren: <?=$todo?></h2>Ingelogd als: <?=$cms->user['human_name']?></center>
<p>

<?
function show_field($field_name, $title, $field_html) {
    echo "<div class=\"form_field\">\n";
    echo "  <label for=\"$field_name\">$title</label>\n";
    echo "  ".$field_html."\n";
    echo "</div>\n";
}

  if($todo == 0) {
    echo "Klaar met ronde 1";
    break;
  }

    echo "<form id=\"jury\" method=\"post\">\n";
    echo "<h3 style=\"text-align:center;\">Voer de beoordelingen van bierkeurmeesters in</h3>";
    echo "<div class=\"form_box\">\n";

    // als je wilt gaan zoeken op handmatig ingevuld label
    // $sql = "SELECT ib.* FROM inschrijving i, inschrijving_bier ib, biertype t where ib.inschrijving=i.id and ib.biertype=t.id and concat(t.label,ib.nummer)='".$_GET['find_inschrijving']."'";

  $klasse="";
  if (isset($_GET['klasse']) && $_GET['klasse'] != "Alles") {
    $klasse = "and t.klasse = ?";
      $values = array($_GET['klasse']);
  }
  else
    $values = array();
  if (!isset($_GET['klasse']))
    $_GET['klasse'] = "Alles";

  $klas = array('A', 'B', 'C', 'D', 'E', 'V', "Alles");
  foreach($klas as $val) {
    $checked = "";
    if ($val == $_GET['klasse']) {
      $checked = "checked";
    }
    $html.= "<label class = \"klasse\">
               <input type = \"radio\"
                 name = \"klasse\"
                 id = \"klasse_$val\"
                 value = \"$val\" $checked/>$val
             </label>";
    }
    show_field('', 'Toon klasse', $html);
    echo "<hr/>";
    show_field('', '', "1e ronde");
    $sql = "select b.id, b.nummer, t.label, i.betaald from inschrijving_bier b left join biertype t on b.biertype=t.id left join inschrijving i on b.inschrijving=i.id where (punten=0 or punten is null) $klasse order by biertype, nummer";
    $result = $cms->db->execute($sql, $values);
    // Wedstrijd nr
    $html = "<select id=\"id\" name=\"id\" class=\"required combo\" style=\"width:300px;\" data-placeholder=\"Selecteer een inzending\">\n";
    $html .= "<option></option>\n";
    while($row = $result->get_nextrow()) {
      if(!$row['betaald'])
        $class = " class=\"red\"";
      else
        $class = "";
      $html .= sprintf(" <option$class value=\"%d\">%s_%02d</option>\n",$row['id'],$row['label'],$row['nummer']);
    }
    $html .= "</select>\n";
    show_field('id', 'Wedstrijd nr.', $html);
    // Keurmeester
    $html = "<select id=\"keurmeester\" name=\"keurmeester\" class=\"required combo\" style=\"width:300px;\" data-placeholder=\"Selecteer een keurmeester\">\n";
    $html .= "<option></option>\n";
    $sql = "select id, voornaam, achternaam from keurmeester order by achternaam, voornaam";
    $keurmeesters = $cms->db->execute($sql);
    while($row = $keurmeesters->get_nextrow())
      $html .= "  <option value=\"$row[id]\">$row[voornaam] $row[achternaam]</option>\n";
    $html .= "</select>\n";
    show_field('keurmeester', 'Keurmeester', $html);
    // Moment keuring
    $html  = "<input class=\"required\" id=\"tijdstip\" name=\"tijdstip\" type=\"time\">\n";
    $html .= "<input type=\"button\" class=\"now\" value=\"Nu\" onclick=\"var now=new Date(); document.getElementById('tijdstip').value = now.getHours()+':'+now.getMinutes();\"/>";
    show_field('tijdstip', 'Tijdstip keuring', $html);
    // Punten
    $html = "<input class=\"required\" id=\"punten\" name=\"punten\" type=\"number\" min=\"0\" max=\"100\">\n";
    show_field('punten', 'Aantal punten', $html);
    // Display a box
    echo "</div>\n";
    echo "<div class=\"form_border\">\n";
    // Visueel
    $html = "<input class=\"required do_sum\" id=\"visueel\" name=\"visueel\" type=\"number\" min=\"0\" max=\"15\"> (max 15 punten)\n";
    show_field('visueel', 'Visuele aspecten', $html);
    // Geur
    $html = "<input class=\"required do_sum\" id=\"geur\" name=\"geur\" type=\"number\" min=\"0\" max=\"25\"> (max 25 punten)\n";
    show_field('geur', 'Geur associaties', $html);
    // Smaak associatie
    $html = "<input class=\"required do_sum\" id=\"smaak_assoc\" name=\"smaak_assoc\" type=\"number\" min=\"0\" max=\"25\"> (max 25 punten)\n";
    show_field('smaak_assoc', 'Smaak associaties', $html);
    // Smaak
    $html = "<input class=\"required do_sum\" id=\"smaak\" name=\"smaak\" type=\"number\" min=\"0\" max=\"35\"> (max 35 punten)\n";
    show_field('smaak', 'Smaak', $html);
    echo "</div>\n";
if ($ronde >= 2) {
    // Keurmeesters
    echo "<hr>\n";
    show_field('', '', "2e ronde");
    $html = "<select multiple id=\"keurmeesters_ronde2\" name=\"keurmeesters_ronde2[]\" class=\"required combo\" style=\"width:300px;\" data-placeholder=\"Selecteer keurmeesters\">\n";
    $html .= "<option></option>\n";
    $keurmeesters->reset();
    while($row = $keurmeesters->get_nextrow())
      $html .= "  <option value=\"$row[id]\">$row[voornaam] $row[achternaam]</option>\n";
    $html .= "</select>\n";
    show_field('keurmeesters_ronde2', 'Keurmeesters', $html);
    $html  = "<input class=\"required\" id=\"tijdstip_ronde2\" name=\"tijdstip_ronde2\" type=\"time\">\n";
    $html .= "<input type=\"button\" class=\"now\" value=\"Nu\" onclick=\"var now=new Date(); document.getElementById('tijdstip_ronde2').value = now.getHours()+':'+now.getMinutes();\"/>";
    show_field('tijdstip_ronde2', 'Tijdstip keuring', $html);
}
if ($ronde == 3) {
    // Keurmeesters
    echo "<hr>\n";
    show_field('', '', "3e ronde");
    $html = "<select multiple id=\"keurmeesters_ronde3\" name=\"keurmeesters_ronde3[]\" class=\"required combo\" style=\"width:300px;\" data-placeholder=\"Selecteer keurmeesters\">\n";
    $html .= "<option></option>\n";
    $keurmeesters->reset();
    while($row = $keurmeesters->get_nextrow())
      $html .= "  <option value=\"$row[id]\">$row[voornaam] $row[achternaam]</option>\n";
    $html .= "</select>\n";
    show_field('keurmeesters_ronde3', 'Keurmeesters', $html);
    $html  = "<input class=\"required\" id=\"tijdstip_ronde3\" name=\"tijdstip_ronde3\" type=\"time\">\n";
    $html .= "<input type=\"button\" class=\"now\" value=\"Nu\" onclick=\"var now=new Date(); document.getElementById('tijdstip_ronde3').value = now.getHours()+':'+now.getMinutes();\"/>";
    show_field('tijdstip_ronde3', 'Tijdstip keuring', $html);
}

    // Verzend knop
    echo "<hr>\n";
    $html = "<input type=\"submit\" class=\"submitButton\" id=\"update\" name=\"update\" value=\"Invoeren\">\n";
    show_field('update', '&nbsp;', $html);
    echo "</div>\n";
    echo "</form>\n";
      
}  
 else {
    echo "U dient ingelogd te zijn";
  } 
?>
<script type="text/javascript">
  $(function() {
    $('select.combo').select2({
      closeOnSelect:false,
      width: 'resolve'
    });
  });
  $(document).ready(function() {
    $('input[name="klasse"]').click( function() {
      // window.location.href=window.location.href+"&klasse="+$(this).val();
      window.location.href=window.location.pathname+"?ronde=<?=$ronde;?>&klasse="+$(this).val();
    });
    $('select#id').change( function() {
      $.getJSON("?ajax_beer="+$(this).val(), function(json) {
        for(var propt in json) {
          if(propt != 'id') {
            var $ob = $('#'+propt);
            if($ob.hasClass('combo'))
              $ob.select2("val",json[propt]).change();
            else
              $ob.val(json[propt]);
          }
        }
      });
    });
    $('form#jury').submit( function() {
      var form_valid = true;
      $(".required").each(function(){
        var val = $(this).val();
        if($(this).hasClass('combo'))
          val = $(this).select2("val");
        if ($.trim(val).length == 0){
          var name = $(this).parent().children('label').text();
          if(!name || name=='')
            var name = this.id;
          alert("Veld '"+name+"' is verplicht!");
          $(this).focus().select2("focus");
          form_valid = false;
          return false;
        }
      });
      if(!form_valid)
        return form_valid;
      $('input[type="number"]').each(function(){
        var max = parseInt($(this).attr('max'));
        if(parseInt($(this).val()) > max) {
          var name = $(this).parent().children('label').text();
          if(!name || name=='')
            var name = this.id;
          alert("De waarde van '"+name+"' mag niet groter zijn dan "+max+"!");
          form_valid = false;
          $(this).focus().select2("focus");
          return false;
        }
      });
      if(!form_valid)
        return form_valid;
      var total = 0;
<? if ($ronde == 1) { ?>
      $(".do_sum").each(function(){
        total += parseInt($(this).val());
      });
      if(total != parseInt($('#punten').val())) {
        alert('De som van de onderdelen moet gelijk zijn aan het aantal punten. Corrigeer dit alvorens op te slaan. Overleg eventueel met de keurmeester!');
        form_valid = false;
        $('#punten').focus().select2("focus");
      }
<? } ?>
      return form_valid;
    });
    return true;
  });
</script>
</div>
<noscript>Javascript is nodig om dit formulier in te kunnen vullen maar is op dit moment niet beschikbaar.</noscript>
<div id="info_popup">
  <div id="info_close">[ X ] &nbsp; </div>
  <div id="info_popup_info">...Momentje...</div>
</div>
</body>
</html>
 
